package com.ogawa.base.base

import android.content.DialogInterface
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.ogawa.base.R
import com.ogawa.base.utils.statusbar.SystemStatusBarUtil
import com.ogawa.base.widget.LoadingDialog
import kotlinx.android.synthetic.main.layout_title_bar.*

abstract class BaseActivity : AppCompatActivity() {
    lateinit var mActivity : BaseActivity
    private var loadingDialog: LoadingDialog? = null
    lateinit var mHandler : Handler
    lateinit var mShowLoadingDialogHandler: Handler
    protected abstract fun getLayoutId(): Int
    protected val TAG = javaClass.name


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mActivity = this
        mHandler = Handler()
        mShowLoadingDialogHandler = Handler()
        setContentView(getLayoutId())
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;//竖屏设置
        SystemStatusBarUtil.setStatusBar(this, R.color.status_bar_def_color, true)
        getWindow().setNavigationBarColor(Color.BLACK)
        initView()
    }

    fun setTitle(title: String) {
        tv_title?.text = title
    }

    protected open fun initView() {
        iv_back?.setOnClickListener { finish() }
    }

    protected fun setBackVis(vis: Boolean) {
        iv_back?.visibility = if (vis) View.VISIBLE else View.GONE
    }

    protected fun setPerVis(vis: Boolean) {
        iv_personal?.visibility = if (vis) View.VISIBLE else View.GONE
    }

    protected fun setMsgVis(vis: Boolean) {
        iv_msg?.visibility = if (vis) View.VISIBLE else View.GONE
    }

    protected fun setAddVis(vis: Boolean) {
        iv_add?.visibility = if (vis) View.VISIBLE else View.GONE
    }
    protected fun setSecondRightImg(id:Int){
        iv_msg?.setImageResource(id)
    }
    protected fun setRightImg(id:Int){
        iv_add?.setImageResource(id)
    }

    protected fun setPerOnclick(onClickListener: View.OnClickListener){
        iv_personal?.setOnClickListener(onClickListener)
    }

    protected fun setMsgOnclick(onClickListener: View.OnClickListener){
        iv_msg?.setOnClickListener(onClickListener)
    }

    protected fun setAddOnclick(onClickListener: View.OnClickListener){
        iv_add?.setOnClickListener(onClickListener)
    }


    fun showLoadingDialog(msg: String = getString(R.string.waiting),canceledOnTouchOutside:Boolean=true) {
        showLoadingDialog(msg,canceledOnTouchOutside,object : DialogInterface.OnDismissListener{
            override fun onDismiss(p0: DialogInterface?) {
            }
        })
    }
    fun lateShowLoading(msg: String = getString(R.string.waiting),canceledOnTouchOutside:Boolean=true) {
        mShowLoadingDialogHandler.postDelayed({
            showLoadingDialog(msg,canceledOnTouchOutside,object : DialogInterface.OnDismissListener{
                override fun onDismiss(p0: DialogInterface?) {
                }
            })
        }, 2000)

    }



    fun showLoadingDialog(msg: String = getString(R.string.waiting),canceledOnTouchOutside:Boolean=true,dismissListener: DialogInterface.OnDismissListener) {
        try{
            if (loadingDialog == null) {
            loadingDialog = LoadingDialog(this,R.style.MyDialogStyle)
        }

            loadingDialog?.setCanceledOnTouchOutside(canceledOnTouchOutside)
            loadingDialog?.setMsg(msg)
            loadingDialog?.setOnDismissListener(dismissListener)
            if (!loadingDialog!!.isShowing) {
                loadingDialog?.show()
            }
        }catch (e:Exception){
            e.printStackTrace()
        }
    }

    fun dismissLoadingDialog() {
        mShowLoadingDialogHandler.removeCallbacksAndMessages(null)
        if (loadingDialog != null && loadingDialog!!.isShowing) {
            loadingDialog!!.dismiss()
        }
    }
}